package com.uex.dev.manage.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Preconditions;
import com.uex.dev.manage.admin.config.MiniserveConfig;
import com.uex.dev.manage.admin.dao.DeviceModelDao;
import com.uex.dev.manage.admin.enums.DeviceTypeEnum;
import com.uex.dev.manage.admin.po.DeviceModelPO;
import com.uex.dev.manage.admin.po.ModelRobotPO;
import com.uex.dev.manage.admin.po.ModelUhubPO;
import com.uex.dev.manage.admin.service.IDeviceModelService;
import com.uex.dev.manage.admin.service.IModelRobotService;
import com.uex.dev.manage.admin.service.IModelUhubService;
import com.uex.dev.manage.admin.utils.LogUtil;
import com.uex.dev.manage.admin.vo.device.DeviceModelResp;
import jakarta.annotation.Resource;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 设备型号表 服务实现类
 * </p>
 *
 * @author wangchao
 * @since 2024-04-11
 */
@Service
public class DeviceModelServiceImpl extends ServiceImpl<DeviceModelDao, DeviceModelPO> implements IDeviceModelService {

    @Resource
    private IModelRobotService modelRobotService;

    @Resource
    private IModelUhubService modelUhubService;

    @Resource
    private MiniserveConfig miniserveConfig;

    @Override
    public List<DeviceModelResp> selectList(String deviceType) {
        ArrayList<DeviceModelResp> modelList = new ArrayList<>();
        LambdaQueryWrapper<DeviceModelPO> wrapper = new LambdaQueryWrapper<DeviceModelPO>()
                .eq(DeviceModelPO::getDeviceType, deviceType)
                .orderByDesc(DeviceModelPO::getId);
        List<DeviceModelPO> list = this.list(wrapper);
        list.forEach(x -> {
            DeviceModelResp devModel = new DeviceModelResp();
            devModel.setModelId(x.getId());
            devModel.setDeviceModel(x.getDeviceModel());
            modelList.add(devModel);
        });
        LogUtil.log(0, "获取设备型号列表");
        return modelList;
    }

    @Override
    public Integer addDeviceModel(DeviceModelPO req) {
        DeviceModelPO deviceModel = new DeviceModelPO();
        LambdaQueryWrapper<DeviceModelPO> wrapper = new LambdaQueryWrapper<DeviceModelPO>()
                .eq(DeviceModelPO::getDeviceType, req.getDeviceType())
                .eq(DeviceModelPO::getDeviceModel, req.getDeviceModel());
        Preconditions.checkArgument(Objects.isNull(this.getOne(wrapper)), "设备型号已存在");
        BeanUtils.copyProperties(req, deviceModel);
        this.save(deviceModel);
        if (req.getDeviceType() == DeviceTypeEnum.UHUB) {
            ModelUhubPO modelUhub = new ModelUhubPO();
            modelUhub.setModelId(deviceModel.getId());
            modelUhubService.save(modelUhub);
        } else {
            ModelRobotPO modelRobot = new ModelRobotPO();
            modelRobot.setModelId(deviceModel.getId());
            modelRobotService.save(modelRobot);
        }
        miniserveMkdirDirectory(req.getDeviceType().toString(), deviceModel.getDeviceModel());
        LogUtil.log(0, "添加设备型号");
        return deviceModel.getId();
    }

    @Override
    public boolean delDeviceModel(Integer modelId) {
        DeviceModelPO deviceModel = this.getById(modelId);
        Preconditions.checkArgument(Objects.nonNull(deviceModel), "设备型号Id不存在");
        if (deviceModel.getDeviceType() == DeviceTypeEnum.UHUB) {
            modelUhubService.removeById(modelId);
        } else {
            modelRobotService.removeById(modelId);
        }
        this.removeById(modelId);
        LogUtil.log(0, "删除设备型号");
        return true;
    }

    private void miniserveMkdirDirectory(String deviceType, String deviceModel) {
        String imgDir = miniserveConfig.getPath() + miniserveConfig.getImg()
                .replace("{devType}", deviceType)
                .replace("{devModel}", deviceModel);
        try {
            FileUtils.forceMkdir(new File(imgDir));
        } catch (Exception e) {
            log.error("[DeviceModelService] mkdir error", e);
        }
    }
}
